package cn.wlinker.video.monitor.enums;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.wlinker.video.monitor.brand.hikversion.HikVisionAccessPlatformEnum;
import cn.wlinker.video.monitor.brand.hikversion.create.net.config.NetConfig;
import cn.wlinker.video.monitor.brand.hikversion.create.net.config.NetConfigList;
import cn.wlinker.video.monitor.domain.vo.FirstLevelDeviceVO;
import cn.wlinker.video.monitor.service.IVideoBrandEnum;
import cn.wlinker.video.monitor.brand.hikversion.create.net.utils.HikCameraUtils;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 依据品牌选择不同的云厂商构建视频流
 *
 * @author gxsjx
 * @version 1.0
 * @date 2022/7/25
 */
@AllArgsConstructor
@Getter
public enum VideoBrandEnum implements IVideoBrandEnum {
    /**
     * 海康：
     * 主码流：/ch1/main/av_stream <br/>
     * 辅码流：/ch1/sub/av_stream
     */
    hk("hk") {
        @Override
        public List<FirstLevelDeviceVO> getNetFirstLevelDeviceList() {
            List<NetConfig> list = NetConfigList.getList();
            if(CollUtil.isNotEmpty(list)){
                return list.stream().map(netConfig -> {
                    FirstLevelDeviceVO firstLevelDeviceVO = new FirstLevelDeviceVO();
                    firstLevelDeviceVO.setCode(netConfig.getCode());
                    firstLevelDeviceVO.setName(netConfig.getName());
                    firstLevelDeviceVO.setMonitorType(this.getMonitorType());
                    firstLevelDeviceVO.setAccessPlatform(HikVisionAccessPlatformEnum.net.getAccessPlatform());
                    return firstLevelDeviceVO;
                }).collect(Collectors.toList());
            }
            return Collections.emptyList();
        }

        @Override
        public String getH264Url(String ip, String username, String password, Integer channelNo) {
            return StrUtil.format("rtsp://{}:{}@{}:554/ch{}/sub/av_stream", username, password, ip, channelNo);
        }

        @Override
        public String getH265Url(String ip, String username, String password, Integer channelNo) {
            return getH264Url(ip, username, password, channelNo);
        }

        @Override
        public String getReplayUrl(String ip, String username, String password, Integer channelNo, Date startTime, Date endTime) {
            String startTimeStr = HikCameraUtils.getRelayTime(startTime);
            String endTimeStr = HikCameraUtils.getRelayTime(endTime);
            return StrUtil.format("rtsp://{}:{}@{}:554/Streaming/tracks/{}01?starttime={}&endtime={}",
                    username, password, ip, channelNo, startTimeStr,endTimeStr);
        }
    },
    /**
     * 大华RTSP：<br/>
     * 主码流：subtype=0 <br/>
     * 辅码流：subtype=1
     */
    dh("dh") {
        @Override
        public String getH264Url(String ip, String username, String password, Integer channelNo) {
            return StrUtil.format("rtsp://{}:{}@{}:554/cam/realmonitor?channel={}&subtype=1", username, password, ip, channelNo);
        }

        @Override
        public String getH265Url(String ip, String username, String password, Integer channelNo) {
            return getH264Url(ip, username, password, channelNo);
        }

        @Override
        public String getReplayUrl(String ip, String username, String password, Integer channelNo, Date startTime, Date endTime) {
            return null;
        }
    },
    /**
     * 宇视：<br/>
     * video1主码流 video2辅码流
     * rtsp://admin:abc123456@172.16.7.144/media/video1/multicast
     */
    ys("ys") {
        @Override
        public String getH264Url(String ip, String username, String password, Integer channelNo) {
            return StrUtil.format("rtsp://{}:{}@{}:554/media/video{}/multicast", username, password, ip,  channelNo);
        }

        @Override
        public String getH265Url(String ip, String username, String password, Integer channelNo) {
            return getH264Url(ip, username, password, channelNo);
        }

        @Override
        public String getReplayUrl(String ip, String username, String password, Integer channelNo, Date startTime, Date endTime) {
            return null;
        }
    };


    private final String monitorType;

    static {
        IVideoBrandEnum.register(VideoBrandEnum.values());
    }

    public static void init() {
    }


    public abstract String getH264Url(String ip, String username, String password, Integer channelNo);

    public abstract String getH265Url(String ip, String username, String password, Integer channelNo);

    public abstract String getReplayUrl(String ip, String username, String password, Integer channelNo, Date startTime, Date endTime);
}
